import com.mysql.jdbc.MysqlDataTruncation;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-03-15
 * Time: 16:30
 */
public class JDBCDemo {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        Scanner scanner=new Scanner(System.in);
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("11111111");
        //2.建立连接
        Connection connection= dataSource.getConnection();
        //3.构造一个操作数据库的sql语句

        System.out.println("请输入学号:");
        int id=scanner.nextInt();
        System.out.println("请输入姓名:");
        String name=scanner.next();

        String sql="insert into test values (?,?)";


        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);

        //4.执行sql
        int n=preparedStatement.executeUpdate();
        System.out.println("n="+n);
        //5.执行完毕,释放资源
        preparedStatement.close();
        connection.close();



    }
}
